package com.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.dao.OrderMasterDao;
import com.shop.entity.OrderMaster;
import com.shop.entity.Result;
import com.shop.enums.OrderMasterStatuEnum;
import com.shop.service.OrderMasterService;
import com.shop.util.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 该文件说明
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/10/12 17:09
 **/
@Service
public class OrderMasterServiceImp implements OrderMasterService {

    @Resource
    private OrderMasterDao orderMasterDao;
    /**
     * description:<分页查找订单>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/12 17:10
     * @param  
     * @return 
     **/
    @Override
    public PageInfo<OrderMaster> findOrderMaster(Integer page, Integer rows, OrderMaster orderMaster) {
        PageHelper.startPage(page,rows);
        List<OrderMaster> list = orderMasterDao.findOrderMaster(orderMaster);
        return new PageInfo<OrderMaster>(list);
    }

    /**
     * description:<修改订单状态>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/15 11:24
     * @param  
     * @return 
     **/
    @Override
    public Result updateOrderMasterStatu(OrderMaster orderMaster) {
        //1.非空判断
        String orderId = orderMaster.getOrderId();
        if( StringUtils.isEmpty(orderId)){
            ResultUtil.error("订单号不能为空");
        }
        try {
            //2.检查是否有该订单号
            OrderMaster findOrderMaster = orderMasterDao.findOrderMasterByOrderId(orderId);
            if(null == findOrderMaster){
                return ResultUtil.error("订单号对应的订单不存在");
            }
            if(findOrderMaster.getOrderStatus().equals(orderMaster.getOrderStatus())){
                return ResultUtil.error("更新订单成功");
            }
            orderMasterDao.updateOrderMasterStatus(orderMaster);
        } catch (Exception e) {
            return ResultUtil.error("更新订单号状态错误，请重试");
        }
        return ResultUtil.success("更新订单号成功");
    }

    /**
     * description:<修改配送状态>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/15 13:59
     * @param  
     * @return 
     **/
    @Override
    public Result updateSendStatus(OrderMaster orderMaster) {
        //1.非空判断
        String orderId = orderMaster.getOrderId();
        if( StringUtils.isEmpty(orderId)){
            ResultUtil.error("订单号不能为空");
        }
        try {
            //2.检查是否有该订单号
            OrderMaster findOrderMaster = orderMasterDao.findOrderMasterByOrderId(orderId);
            if(null == findOrderMaster){
                return ResultUtil.error("订单号对应的订单不存在");
            }
            if(findOrderMaster.getSendStatus().equals(orderMaster.getSendStatus())){
                return ResultUtil.error("更新订单成功");
            }
            orderMasterDao.updateSendStatus(orderMaster);
        } catch (Exception e) {
            return ResultUtil.error("更新订单号状态错误，请重试");
        }
        return ResultUtil.success("更新订单号成功");
    }
}
